<?PHP

function files_allowed_types() {
	//auth_club_action_access();
	include(DOC_ROOT."/include/files.inc.php");
	foreach ($files["formats"] as $key=>$mime){
		$ext[$key] = $mime;
	}
	return $ext;

}//files_allowed_types
function file_auth( $fileinfo ) {

	$oextension = str_replace(".","",strtolower( substr( $fileinfo["name"], strrpos( $fileinfo["name"], "." ) ) ));
	$formats = files_allowed_types();
	$extension = "";
	$mimes = array();

	while(list($key,$val) = each($formats)){
		if( $oextension == $key ){
			$mimes = explode("|",$val);
			foreach($mimes as $mime){
				$file["mime"][] = $mime;
			}
			$file["ext"] = $key;
		}//if
	}//while
	//print_r($fileinfo);
	//echo $file["mime"] ."=".$fileinfo["type"]." & ".$file["ext"]. " - ".$oextension;

	//CHECK UPLOAD LIMITS;
	include(DOC_ROOT."/include/files.inc.php");

	if($fileinfo["size"] > $files["maxsize"] * 1048576)
		load_page("register.php?msg_id=301");

	$replace["mime_type"] = $fileinfo["type"];
	//print_r($fileinfo);
	//	echo "<hr>";
	//	print_r($file);
		//exit;
	if(is_array($file["mime"])){
		
		if(in_array($fileinfo["type"],$file["mime"]) && $file["ext"])
			return $file["ext"];
		else
			load_page("register.php?msg_id=302");
	}
	else{
		load_page("register.php?msg_id=302");
	}

}
function file_process($values){
	
	$extension  = file_auth($values);

	$file_name = $_SESSION['uid'];
	
	if($_SESSION['uid'] > 999)
		$dir1 = substr($_SESSION['uid'],0,-3);
	else
		$dir1 = 0;
	
	if(!file_exists(FILE_ROOT."/".$dir1)){
		mkdir(FILE_ROOT."/".$dir1);
		//chmod(FILE_ROOT."/docs/".$dir1,0755);
	}
	$fullname = FILE_ROOT."/".$dir1."/".$file_name.".".$extension;
	if(file_exists($fullname)){
			@unlink($fullname);
	}

	move_uploaded_file($values["tmp_name"],$fullname);
	
	return $dir1."/".$file_name.".".$extension;
	
}
function extract_mimetype($file){

	$mime_types=array();
	include_once(DOC_ROOT."/include/files.inc.php");
	$mime_types = $files["formats"];
	$ext=strtolower(substr(strrchr($file,'.'),1));
	if($ext && array_key_exists($ext,$mime_types)){
	   // if so, grab the content type
	   $mime=$mime_types[$ext];
	}else{
	   // otherwise, create an error for that
	   $mime="application/force-download";
	}

	return $mime;
}
?>